#ifndef STATIONANDCITYDELEGATE_H
#define STATIONANDCITYDELEGATE_H
#include <QComboBox>
#include <QStyledItemDelegate>
#include <QPainter>
#include <QApplication>
const int StationTypeRole = Qt::UserRole + 5;
class StationAndCityDelegate:public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit StationAndCityDelegate(QObject* parent = nullptr):QStyledItemDelegate(parent){
    }
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
        painter->save();
        QString stationName = index.data(Qt::DisplayRole).toString(); // 显示文本
        QString stationType = index.data(StationTypeRole).toString(); // 隐藏的类型
        QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &option, painter);
        QIcon icon;
        //qDebug()<<stationType;
        if(stationType=="TrainStation"){
            icon = QIcon(":/res/icons/trainstation.ico");
        }else if(stationType=="CarStation"){
            icon = QIcon(":/res/icons/carstation.ico");
        }else if(stationType=="PlaneStation"){
            icon = QIcon(":/res/icons/planestation.ico");
        }
        QRect iconRect = option.rect.adjusted(4, 4, -option.rect.width() + 52, -4);
        icon.paint(painter, iconRect, Qt::AlignLeft | Qt::AlignVCenter);
        QRect textRect = option.rect.adjusted(60, 0, 0, 0);
        painter->setPen(Qt::red);
        painter->drawText(textRect, Qt::AlignVCenter | Qt::TextSingleLine, stationName);
        painter->restore();
    }
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{
        return QSize(option.rect.width(),50);
    }
};

#endif // STATIONANDCITYDELEGATE_H
